package com.as.servlet;

import com.as.utils.QRCodeUtils;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;

/**
 * Created by 汪贵春 on 2017/2/28.
 */
@WebServlet(name = "QRCode", urlPatterns = "/QRCode")
public class QRCodeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String qrCodeString = "http://192.168.0.2:8080/login/" + request.getParameter("qid");

        ServletOutputStream stream = response.getOutputStream();
        BitMatrix m = QRCodeUtils.getBitMatrix(qrCodeString);

        MatrixToImageConfig config = new MatrixToImageConfig(Color.black.getRGB(), Color.white.getRGB());
        MatrixToImageWriter.writeToStream(m, "png", stream, config);
        stream.flush();
        stream.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
